#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
#include "struct.h"
#include "window.h"

// 3.增加窗口
int Window_add(Restaurant *pR) {
	
	// 如果当前餐厅有效窗口已满，则退出 
	if(pR->window_count >= MAX_WINDOW_PER_RESTAURANT) { 
		printf("该餐馆当前窗口已满，无法添加！\n");
		return 0;
	}
	 
	// window_count初始为0 
	int new_index = pR->window_count;      // 记录新窗口索引
		
	printf("新增窗口：\n");
 	printf("请输入窗口名称：");
	scanf("%29s", pR->windows[new_index].window_name);
 
 
 	pR->windows[new_index].window_state = 1;  // 当前窗口营业 
 	pR->windows[new_index].dish_count = 0;    // 当前窗口无菜品 
	pR->windows[new_index].order_count = 0;   // 当前窗口无订单
	pR->window_count++;                       // 信息添加完成，当前有效窗口数自增
	
	printf("窗口添加成功！\n"); 
	return 1;
	
}

// 4. 删除窗口
int Window_delete(Restaurant *pR) {
	
	// 如果当前餐厅没有任何有效窗口，则退出 
	if(pR->window_count == 0) {
		printf("该餐馆没有任何窗口，无法删除！\n");
		return 0;
	}
		
	// 展示当前的窗口方便删除
	printf("\n该餐馆所有窗口如下：");
    printf("\n%-10s%-15s%-10s%-10s%-10s\n",
           "窗口序号", "  窗口名称", "  菜品数", "  订单数", "  状态");
    printf("--------------------------------------------\n");
    for (int i = 0; i < pR->window_count; i++) {
        Window *win = &pR->windows[i];
        printf("%d        %s      %d      %d       %s\n",
               i+1,
               win->window_name,
               win->dish_count,
               win->order_count,
               win->window_state == 1 ? "营业" : "已打烊");
    }
    printf("--------------------------------------------\n");
	

	int id; 
	int found_index = -1; 
	do {
		printf("\n请输入想要删除的窗口号：");
		int result = scanf("%d", &id);
		if(result != 1 || id < 1 || id > 10) {
			printf("只能输入当前有效的窗口，请重新输入！\n");
			while (getchar() != '\n');
			continue;
		}
		
		// 查找输入的窗口是否存在 
		found_index = -1; 
		for (int i = 0; i < pR->window_count; i++) {   // 数组索引为0-9 
			if(i == id - 1){ 
				found_index = i;  // 记录数组索引  
				break;
			}
		}
		
		if(found_index == -1) {
			printf("没能找到%d号窗口，请重新输入！\n", id);
		}
		
	}
	while(found_index == -1);  //直到找到有效id才退出循环 
	
	// 如果找到便删除，将后面的窗口前移
	for(int i = found_index; i < pR->window_count-1; i++) {
		pR->windows[i] = pR->windows[i+1];
	} 
	
	pR->window_count--; // 当前有效窗口数自减
	printf("%d号窗口已成功删除！\n", id); 
	return 1;
	
}

// 5. 更新窗口信息
int Window_update(Restaurant *pR) {
	
	// 如果当前餐厅没有任何有效窗口，则退出 
	if(pR->window_count == 0) {
		printf("该餐馆没有任何窗口，无法更改！\n");
		return 0;
	}
	
	// 展示当前的窗口方便删除
	printf("\n该餐馆所有窗口如下：");
	
    printf("\n%-10s%-15s%-10s%-10s%-10s\n",
           "窗口序号", "  窗口名称", "  菜品数", "  订单数", "  状态");
    printf("--------------------------------------------\n");
    for (int i = 0; i < pR->window_count; i++) {
        Window *win = &pR->windows[i];
        printf("%d        %s      %d      %d       %s\n",
               i+1,
               win->window_name,
               win->dish_count,
               win->order_count,
               win->window_state == 1 ? "营业" : "已打烊");
    }
    printf("--------------------------------------------\n");

	int id;  // 需要更改的窗口id 
	int found_index = -1;  
	do {
		printf("\n请输入想要更新的窗口号：");
		int result = scanf("%d", &id);                                
		if(result != 1 || id < 1 || id > 10) {	
			printf("只能输入当前有效的窗口，请重新输入！\n");
			while (getchar() != '\n');
			continue;
		}
		
		// 查找输入的窗口是否存在 
		found_index = -1; 
		for (int i = 0; i < pR->window_count; i++) {   // 数组索引为0-9 
			if(i == id - 1){ 
				found_index = i;  // 记录数组索引  
				break;
			}
		}
		
		if(found_index == -1) {
			printf("没能找到%d号窗口，请重新输入！\n", id);
		}
		
	}
	while(found_index == -1); 
	 
	printf("\n%d号窗口的信息如下：\n", id);
	printf("窗口名称：%s\n", pR->windows[found_index].window_name);	
	printf("营业状态：%s\n", pR->windows[found_index].window_state ? "营业" : "打烊");
		
	int choice;
	
	do {
		printf("\n请选择要更新的字段（输入0结束更新）：\n");
        printf("1. 窗口名称\n");
        printf("2. 营业状态\n");
        printf("0. 结束更新\n");
        printf("选择修改项：");
        scanf("%d", &choice);
	 	
	 	switch(choice) {
	 		
	 		case 1: {
	 			char new_name[30];
			 	printf("请输入新名称：");
			 	scanf("%29s", new_name);
			 	strcpy(pR->windows[found_index].window_name, new_name);
			 	printf("%d号窗口的名称已经更新为：%s\n", id, new_name);

				break;
			}
			 
			 case 2: {
			 	
			 	int new_state;
			 	do {
			 		
			 		printf("请输入新的营业状态（1=营业，0=打烊）：");
					scanf("%d", &new_state);
					
					if(new_state != 1 && new_state != 0) {
						printf("无效，请输入正确的营业状态！\n"); 
					}
				} 
				while(new_state != 1 && new_state != 0);
			 	
			 	pR->windows[found_index].window_state = new_state;
			 	printf("%d号窗口的营业状态已更改成：%s\n", id, new_state ? "营业" : "打烊"); 
				break;
			}
			 
			case 0: {
			 	
			 	printf("窗口信息更新结束！\n"); 
				break;
			}
			
			default: {
				
				printf("无效选择，请重新输入！");	
				break;
			}
		}
	}
	while(choice != 0);
	return 1;
}
